package EJBTool.persistent.edit.policies;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.ui.commands.DeferredLayoutCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalConnectionEditPolicy;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;

/**
 * @generated
 */
public class PersistentViewCanonicalEditPolicy extends
		CanonicalConnectionEditPolicy
{

	/**
	 * @generated
	 */
	Set myFeaturesToSynchronize;

	/**
	 * @generated
	 */
	protected List getSemanticChildrenList()
	{
		View viewObject = (View) getHost().getModel();
		List result = new LinkedList();
		for (Iterator it = EJBTool.persistent.part.EJBToolDiagramUpdater
				.getRoot_79SemanticChildren(viewObject).iterator(); it
				.hasNext();)
		{
			result.add(((EJBTool.persistent.part.EJBToolNodeDescriptor) it
					.next()).getModelElement());
		}
		return result;
	}

	/**
	 * @generated
	 */
	protected boolean shouldDeleteView(View view)
	{
		return true;
	}

	/**
	 * @generated
	 */
	protected boolean isOrphaned(Collection semanticChildren, final View view)
	{
		int visualID = EJBTool.persistent.part.EJBToolVisualIDRegistry
				.getVisualID(view);
		switch (visualID)
		{
			case EJBTool.persistent.edit.parts.EntityBeanEditPart.VISUAL_ID:
				return !semanticChildren.contains(view.getElement())
						|| visualID != EJBTool.persistent.part.EJBToolVisualIDRegistry
								.getNodeVisualID((View) getHost().getModel(),
										view.getElement());
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected String getDefaultFactoryHint()
	{
		return null;
	}

	/**
	 * @generated
	 */
	protected Set getFeaturesToSynchronize()
	{
		if (myFeaturesToSynchronize == null)
		{
			myFeaturesToSynchronize = new HashSet();
			myFeaturesToSynchronize.add(EJBTool.EJBToolPackage.eINSTANCE
					.getRoot_EntityBeans());
		}
		return myFeaturesToSynchronize;
	}

	/**
	 * @generated
	 */
	protected List getSemanticConnectionsList()
	{
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	protected EObject getSourceElement(EObject relationship)
	{
		return null;
	}

	/**
	 * @generated
	 */
	protected EObject getTargetElement(EObject relationship)
	{
		return null;
	}

	/**
	 * @generated
	 */
	protected boolean shouldIncludeConnection(Edge connector,
			Collection children)
	{
		return false;
	}

	/**
	 * @generated
	 */
	protected void refreshSemantic()
	{
		List createdViews = new LinkedList();
		createdViews.addAll(refreshSemanticChildren());
		List createdConnectionViews = new LinkedList();
		createdConnectionViews.addAll(refreshSemanticConnections());

		if (createdViews.size() > 1)
		{
			// perform a layout of the container
			DeferredLayoutCommand layoutCmd = new DeferredLayoutCommand(host()
					.getEditingDomain(), createdViews, host());
			executeCommand(new ICommandProxy(layoutCmd));
		}

		createdViews.addAll(createdConnectionViews);
		makeViewsImmutable(createdViews);
	}

	/**
	 * @generated
	 */
	private Diagram getDiagram()
	{
		return ((View) getHost().getModel()).getDiagram();
	}

}
